package com.dingwei.superiordiary.ui.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.dingwei.superiordiary.R;
import com.dingwei.superiordiary.adapter.adapter.BrowseHistoryAdapter;
import com.dingwei.superiordiary.bean.BrowseHistoryBean;
import com.dingwei.superiordiary.mvp.presenter.BrowseHistoryPresenter;
import com.dingwei.superiordiary.mvp.view.Contract;
import com.dingwei.superiordiary.utils.DialogUtil;
import com.jude.easyrecyclerview.adapter.RecyclerArrayAdapter;
import com.lihao.baselibrary.base.BaseActivity;
import com.lihao.baselibrary.utils.MathUtil;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by lihao on 2018/1/11.
 */

public class BrowseHistoryActivity extends BaseActivity implements Contract.BrowseHistoryView {
    BrowseHistoryPresenter browseHistoryPresenter = new BrowseHistoryPresenter(this);
    @Bind(R.id.img_back)
    ImageView imgBack;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.tv_right)
    TextView tvRight;
    @Bind(R.id.m_recyclerView)
    RecyclerView mRecyclerView;
    @Bind(R.id.refresh_layout)
    SmartRefreshLayout refreshLayout;
    @Bind(R.id.no_data_layout)
    RelativeLayout noDataLayout;


    private BrowseHistoryAdapter adapter;
    private int page = 1;


    @Override
    protected int getContentViewId() {
        return R.layout.activity_browse_history;
    }

    @Override
    protected void initData() {
        refreshLayout.autoRefresh();
    }

    protected void initView() {
        tvTitle.setText("浏览历史");
        tvRight.setText("清空");

        LinearLayoutManager layoutManager = new LinearLayoutManager(context);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(layoutManager);
        mRecyclerView.setNestedScrollingEnabled(false);
        adapter = new BrowseHistoryAdapter(this);
        mRecyclerView.setAdapter(adapter);
    }

    @Override
    protected void initListener() {
        adapter.xxInterfaceListener(new BrowseHistoryAdapter.xxInterFace() {
            @Override
            public void delete(int position) {
                BrowseHistoryBean.DataBean bean = adapter.getItem(position);
                browseHistoryPresenter.delete(bean.id,position);
            }
        });

        refreshLayout.setOnRefreshLoadmoreListener(new OnRefreshLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                page = page + 1;
                browseHistoryPresenter.initData(page,false,false);
            }

            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                page = 1;
                browseHistoryPresenter.initData(page,true,false);
            }
        });
        adapter.setOnItemClickListener(new RecyclerArrayAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                BrowseHistoryBean.DataBean  bean = adapter.getItem(position);
                intent  = new Intent(BaseActivity.context, CommodityDetailsActivity.class);
                intent.putExtra("id",bean.id);
                startActivity(intent);
            }
        });
    }


    public void showData() {
        if (adapter.getAllData() == null || adapter.getAllData().size() == 0) {
            noDataLayout.setVisibility(View.VISIBLE);
            mRecyclerView.setVisibility(View.GONE);
        } else {
            mRecyclerView.setVisibility(View.VISIBLE);
            noDataLayout.setVisibility(View.GONE);
        }
    }

    @OnClick({R.id.img_back, R.id.tv_right})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_back:
                finish();
                break;
            case R.id.tv_right:
                DialogUtil.showAlertDialogChoose(BaseActivity.context, "温馨提示", "删除内容后其中的所有内容都会被删除", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        switch (which) {
                            case DialogUtil.BUTTON1:
                                dialog.dismiss();
                                break;
                            case DialogUtil.BUTTON2:
                                dialog.dismiss();
                                break;
                        }
                    }
                });
                break;
        }
    }

    @Override
    public void onFail() {
        refreshLayout.finishRefresh();
        refreshLayout.finishLoadmore();
        showData();
    }

    @Override
    public void onRefresh(BrowseHistoryBean bean) {
        adapter.clear();
        adapter.addAll(bean.data);
        refreshLayout.finishRefresh();
        showData();
    }

    @Override
    public void onLoadMore(BrowseHistoryBean bean) {
        adapter.addAll(bean.data);
        refreshLayout.finishLoadmore();
        showData();
    }

    @Override
    public void deleteSuccess(int position) {
        adapter.remove(position);
    }
}
